﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using SF.Code.Extension.DebuggingHelper;
using SG.Productivity.DesktopControlPanel.ParallelQuery;

namespace SG.Productivity.DesktopControlPanel.Controls
{
    public partial class QueryMatchControl : UserControl, IActivatableItemControl
    {
        private QueryMatch _match;

        public QueryMatchControl()
        {
            InitializeComponent();
        }

        public QueryMatch Match
        {
            get { return _match; }
            set
            {
                _match = value;
                Name = Uri.ToString();
                label1.Text = Title;
                linkLabel1.Text = _match.Uri.ToString();
                fiveStarControl1.Rating = (decimal)Match.Weight;
            }
        }

        public Uri Uri
        {
            get { return Match.Uri; }
        }

        public string Title
        {
            get
            {
                var path = Uri.PathAndQuery;

                string displayName;
                if (Uri.Scheme == "file")
                {
                    displayName = Uri.LocalPath;
                }
                else
                {
                    var slashIndex = path.LastIndexOf('/');
                    displayName = Uri.UnescapeDataString(slashIndex >= 0 ? path.Substring(slashIndex + 1) : path);
                }

                return !string.IsNullOrWhiteSpace(path) ? displayName : Uri.ToString();
            }
        }

        public QueryIdentifier Identifier { get; set; }

        public void SetActive(bool active)
        {
            if (active)
            {
                BackColor = Color.CornflowerBlue;

                var parent = Parent as ScrollableControl;
                if (parent != null)
                    parent.ScrollControlIntoView(this);
            }
            else
            {
                BackColor = DefaultBackColor;
            }
        }
        #region Custom Events

        [Category("Behavior")]
        [Description("Fires when this control requests activation by it's container")]
        public event EventHandler<RequestActivationEventArgs> RequestActivation;

        protected virtual void OnRequestActivation(bool active)
        {
            var requestAction = RequestActivation;
            if (requestAction != null)
                requestAction(this, new RequestActivationEventArgs(active));
        }

        [Category("Behavior")]
        [Description("Fires when the URI is invoked")]
        public event EventHandler<LinkLabelLinkClickedEventArgs> LinkClicked;

        protected virtual void OnLinkClicked(LinkLabelLinkClickedEventArgs e)
        {
            OnRequestActivation(true);

            try
            {
                Process.Start(new ProcessStartInfo(Uri.ToString()) { UseShellExecute = true });
            }
            catch (Exception exception)
            {
                exception.Log();
            }


            var linkClicked = LinkClicked;
            if (linkClicked != null)
                linkClicked(this, e);
        }

        [Category("Behavior")]
        [Description("Fires when the user changes the rating")]
        public event EventHandler<RatingChangedEventArgs> RatingChanged;

        protected virtual void OnRatingChanged(RatingChangedEventArgs e)
        {
            var ratingChanged = RatingChanged;
            if (ratingChanged != null)
                ratingChanged(this, e);
        }

        #endregion

        private void fiveStarControl1_RatingChanged(object sender, RatingChangedEventArgs e)
        {
            OnRatingChanged(e);
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            OnLinkClicked(e);
        }

        private void QueryMatchControl_MouseClick(object sender, MouseEventArgs e)
        {
            OnRequestActivation(true);
        }
    }
}

